package com.itzdm.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itzdm.reggie.common.Result.Result;
import com.itzdm.reggie.common.basecontext.BaseContext;
import com.itzdm.reggie.entity.AddressBook;
import com.itzdm.reggie.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Api(tags = "地址簿相关接口")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址接口
     * @param addressBook
     * @return
     */
    @PostMapping
    @ApiOperation("新增地址接口")
    public Result<AddressBook> save(@RequestBody AddressBook addressBook) {
        log.info("新增地址：{}", addressBook);
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return Result.success(addressBook);
    }

    /**
     * 默认地址接口
     * @param addressBook
     * @return
     */
    @PutMapping("default")
    @ApiOperation("默认地址接口")
    public Result<AddressBook> setDefault(@RequestBody AddressBook addressBook) {
        log.info("默认地址：{}", addressBook);
        addressBookService.setDefault(addressBook);
        return Result.success(addressBook);
    }

    /**
     * 根据id查询地址
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result get(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        if(addressBook == null){
            return Result.error("未找到信息");
        }else {
            return Result.success(addressBook);
        }
    }

    @GetMapping("default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> getDefault(){
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookService.getOne(queryWrapper);
        if(addressBook == null){
            return Result.error("未找到信息");
        }else {
            return Result.success(addressBook);
        }
    }

    /**
     * 查询指定用户的全部列表
     * @param addressBook
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询指定用户的全部列表")
    public Result<List<AddressBook>> list(AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());

        //构造条件构造器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(addressBook.getUserId() != null,AddressBook::getUserId, addressBook.getUserId());
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);
        return Result.success(addressBookService.list(queryWrapper));
    }


    /**
     * 删除地址
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除地址")
    public Result<String> delete(@RequestParam List<Long> ids){
        addressBookService.removeByIds(ids);
        return Result.success("删除成功");
    }

    /**
     * 更新地址接口
     * @param addressBook
     * @return
     */
    @PutMapping
    @ApiOperation("更新地址接口")
    public Result<AddressBook> update(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return Result.success(addressBook);
    }
}
